package com.shanghaionstar.nav.adapter;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.onstar.cn.SharedPoiItemInfo;
import com.onstar.cn.common.util.DeviceUtil;
import com.onstar.cn.framework.annotation.FindViewById;
import com.onstar.cn.framework.base.CommonAdapter;
import com.onstar.cn.framework.base.CommonViewHolder;
import com.onstar.cn.sqlite.entities.PoiSearchHistory;
import com.onstar.cn.map.common.PoiItemInfo;
import com.onstar.cn.ripple.view.RippleCustomBtn;
import com.onstar.cn.rxconstants.RxConstants;
import com.shanghaionstar.R;
import com.shanghaionstar.iview.ILoginListener;
import com.shanghaionstar.nav.presenter.NavMainPresenter;
import com.shanghaionstar.utils.LoginUtil;

import java.util.List;

public class NavPoiSearchHistoryAdapter extends CommonAdapter<PoiSearchHistory> implements ILoginListener {
    private List<PoiSearchHistory> historyOrFavoriteInfoLists;
    private PoiSearchHistory poiSearchHistorys;
    private boolean isShowTbt;

    private NavMainPresenter navMainPresenter;

//    public NavPoiSearchHistoryAdapter(List<PoiSearchHistory> datas, Context context, NavGetSearchHistoryPresenter navGetSearchHistoryPresenter) {
//        super(datas, context);
//        this.context = context;
//        this.historyOrFavoriteInfoLists = datas;
//        this.navGetSearchHistoryPresenter = navGetSearchHistoryPresenter;
//    }

    public NavPoiSearchHistoryAdapter(List<PoiSearchHistory> datas, Context context, NavMainPresenter navMainPresenter, boolean isShowTbt) {
        super(datas, context);
        this.historyOrFavoriteInfoLists = datas;
        this.isShowTbt = isShowTbt;
        this.navMainPresenter = navMainPresenter;
    }

    @Override
    public void getView(final int position, CommonViewHolder viewHolder) {
        ViewHolder holder = (ViewHolder) viewHolder;
        poiSearchHistorys = new PoiSearchHistory();
        poiSearchHistorys = historyOrFavoriteInfoLists.get(position);

        if (DeviceUtil.getLanguage().equalsIgnoreCase("en")) {
            holder.btn_sendtbt.setTextSize(12f);
        } else {
            holder.btn_sendtbt.setTextSize(14f);
        }
        if (!isShowTbt) {
            holder.btn_sendtbt.setVisibility(View.GONE);
            holder.text_vendor_name.setText(poiSearchHistorys.getName());
            holder.text_vendor_addres.setText(poiSearchHistorys.getAddress());
        } else {
            holder.text_vendor_name.setText(poiSearchHistorys.getName());
            holder.text_vendor_addres.setText(poiSearchHistorys.getAddress());

            holder.btn_sendtbt.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("poiSearchHistory", historyOrFavoriteInfoLists.get(position));
                    new LoginUtil().loginAndActionCallBack(context, RxConstants.NAV_SEARCH_HISTROY_PAGE, NavPoiSearchHistoryAdapter.this, bundle);
                }
            });

//            holder.viewoitem.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    BusinessUtil.sendReportByClickType(context,
//                            FunctionNameFindId.Searchpoi_clickfavorite.getStringValue());
//                    FavoriteEntity favoriteEntity = list.get(position);
//                    PoiItemInfo info = new PoiItemInfo();
//                    info.setTitle(favoriteEntity.getPoiName());
//                    info.setCityName(favoriteEntity.getCityCode());
//                    info.setCityCode(favoriteEntity.getPoiCityCode());
//                    info.setId(favoriteEntity.getFavoriteDestinationID());
//                    info.setLatitude(favoriteEntity.getPoiCoordinate().getLatitude());
//                    info.setLongitude(favoriteEntity.getPoiCoordinate().getLongitude());
//                    info.setSnippet(favoriteEntity.getPoiAddress());
//                    info.setTelphone(favoriteEntity.getPoiPhoneNumber());
//                    info.setTypeDes(favoriteEntity.getPoiCatetory());
//
//                    String city = info.getCityName();
//                    if (DeviceUtil.getLanguage().equalsIgnoreCase("en")) {
//                        LoadCityList cityList = new LoadCityList(context);
//                        city = cityList.findByZhCity(info.getCityName());
//                    }
//                    info.setCityName(city);
//                    returnLevel(info);
//                }
//            }
        }
    }


    public CommonViewHolder getViewHolder() {
        return new ViewHolder();
    }

    @Override
    public int layoutId() {
        return R.layout.nav_history_fav_listview_item;
    }

    private class ViewHolder extends CommonViewHolder {
        @FindViewById(R.id.text_vendor_name)
        TextView text_vendor_name;
        @FindViewById(R.id.text_vendor_addres)
        TextView text_vendor_addres;
        @FindViewById(R.id.btn_sendtbt)
        RippleCustomBtn btn_sendtbt;
    }

    public void sendToCar(SharedPoiItemInfo poiItemInfo){
        Bundle bundle = new Bundle();
        bundle.putSerializable("poiSearchHistory", poiItemInfo);
        new LoginUtil().loginAndActionCallBack(context, RxConstants.NAV_SEARCH_HISTROY_PAGE, NavPoiSearchHistoryAdapter.this, bundle);
    }

    @Override
    public void onLogin(Bundle bundle, String resultCode) {
        if (bundle != null) {
            if (RxConstants.ACTION_SUCCESS.equals(resultCode)) {
//                BusinessUtil.sendReportByClickType(context, FunctionNameFindId.Searchpoi_history_navigation.getStringValue());
                PoiSearchHistory poiSearchHistory;
                PoiItemInfo poiItemInfo = new PoiItemInfo();
                Object object = bundle.getSerializable("poiSearchHistory");
//                LogManager.getInstance().d("sendToCar"+object.getClass().getSimpleName());
                if (object instanceof SharedPoiItemInfo){
                    poiItemInfo = (SharedPoiItemInfo)object;
                }else if (object instanceof PoiSearchHistory){
                    poiSearchHistory = (PoiSearchHistory)object;
                    poiItemInfo.setTitle(poiSearchHistory.getName());
                    poiItemInfo.setThreadId(poiSearchHistory.getSearchtime());
                    poiItemInfo.setAddress(poiSearchHistory.getAddress());
                    poiItemInfo.setLatitude(poiSearchHistory.getLat());
                    poiItemInfo.setLongitude(poiSearchHistory.getLon());
                    poiItemInfo.setCityName(poiSearchHistory.getCityname());
                    poiItemInfo.setTelphone(poiSearchHistory.getPhone());
                }
                navMainPresenter.sendTbtOrOdd(poiItemInfo);
            }
        }
    }
}
